﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Intermediate.Data;
using Shop.Intermediate.Repository;
using Shop.Core.Configuration;
using DoNet.Common.Utilities;
using Shop.Intermediate;

namespace Shop.Core
{
    public class DepartmentCore:IPageSplit
    {
        private IDataProvider m_dataProvider;
        private ITransaction  m_transaction;
        private IDepartmentRepository m_dptRespository;

        public DepartmentCore()
        {
            this.m_dataProvider = ConfigAccess.CreateDataProvider();
            this.m_dptRespository = m_dataProvider.DepartmentRepository;
        }

        public bool SetMananger(string code, string userCode,ref string message)
        {
            bool result = true;
            IDepartment dptEntity = m_dptRespository.GetEntity(code);
            if (dptEntity.Magager.Equals(userCode))
            {
                result = false;
                message = "主管领导已经是" + userCode;
            }
            else
            {
                dptEntity.Magager = userCode;
                m_dptRespository.Update(dptEntity);
                m_dptRespository.SubmitChanges();
                message = "设置成功！";
            }
            return result;
        }

        public string GetManager(string code)
        {
            IDepartment dptEntity = m_dptRespository.GetEntity(code);
            string result=string.Empty;
            if (dptEntity != null)
            {
                result = dptEntity.Magager;
            }
            return result;
        }

        public bool SetState(string code, bool state,ref string message)
        {
            bool result = true;
            message = "更新成功！";
            IDepartment dptEntity = m_dptRespository.GetEntity(code);
            if (dptEntity.State == state)
            {
                message = "状态已经是" + state.ToString();
                result = false;
            }
            else
            {
                dptEntity.State = state;
                m_dptRespository.Update(dptEntity);
                m_dptRespository.SubmitChanges();
                message = "设置成功！";
            }
            return result;
        }

        public void ChangeSequence(string code, int Sequence)
        {
            m_transaction = this.m_dataProvider.Transaction;
            m_transaction.Begin();
            IDepartment dptEntity = m_dptRespository.GetEntity(code);
            int? currentSEQ = dptEntity.Sequence;
            dptEntity.Sequence = Sequence;
            try
            {
                this.m_dptRespository.Update(dptEntity);
                //所有大于等于Sequence>=currentSEQ的dptEntity 的Sequence 都加1
                var query = from p in m_dptRespository.GetList()
                            where p.Sequence >= currentSEQ
                            select p;
                foreach (var a in query)
                {
                    a.Sequence = a.Sequence+1;
                    m_dptRespository.Update(a);
                }
                
                this.m_dptRespository.SubmitChanges();

                this.m_transaction.Commit();
            }
            catch(Exception ex)
            {
                this.m_transaction.Rollback();
                throw (new Exception(ex.ToString()));
            }
        }

        public bool Create(IDepartment entity, ref string message)
        {
            bool result = true;
            IDepartment en;
            en = m_dptRespository.GetEntity(entity.Code);
            if (en != null)
            {
                result = false;
                message = "存在相同的代码！\\n";
            }
            en = m_dptRespository.GetList(" Name==@0 ", new object[] { entity.Name }).FirstOrDefault();
            if (en != null)
            {
                result = false;
                message = message + "存在相同的名称！";
            }

            if (result == true)
            {
                m_dptRespository.Create(entity);
                m_dataProvider.SubmitChanges();
                message = "添加成功！";
            }
            return result;
        }

        public bool Update(IDepartment entity, ref string message)
        {
            bool result = true;
            IDepartment en;
            en = m_dptRespository.GetEntity(entity.Code);
            if (en != null)
            {
                result = false;
                message = "存在相同的代码！\\n";
            }
            en = m_dptRespository.GetList(" Name==@0 && Code!=@1", new object[] { entity.Name,entity.Code }).FirstOrDefault();
            if (en != null)
            {
                result = false;
                message = message + "存在相同的名称！";
            }

            if (result == true)
            {
                m_dptRespository.Update(entity);
                m_dataProvider.SubmitChanges();
                message = "添加成功！";
            }
            return result;
        }

        public IDepartment NewEntity()
        {
            return m_dataProvider.NewDepartment();
        }

        public IEnumerable<IDepartment> GetList()
        {
            return m_dptRespository.GetList();
        }

        public IEnumerable<IDepartment> PageResultQuery<IDepartment>(string condition, object[] whereObj, string ordering, object[] orderObj, int pageNo, int pageSize, out int pageCount, out int recordCount)
        {
            return m_dptRespository.PageResultQuery<IDepartment>(condition, whereObj, ordering, orderObj, pageNo, pageSize, out pageCount, out recordCount);
        }

    }

}
